package com.ruoyi.web.controller.api;

import com.ruoyi.cat.domain.CatApplycashoutTable;
import com.ruoyi.cat.domain.CatChance;
import com.ruoyi.cat.domain.CatShareuserTable;
import com.ruoyi.cat.domain.DTO.ActivityShareDTO;
import com.ruoyi.cat.domain.DTO.ActivityTableDTO;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.domain.DTO.MyTeamDTO;
import com.ruoyi.cat.domain.VO.ApiSaveShareVO;
import com.ruoyi.cat.service.*;
import com.ruoyi.cat.service.impl.RichTextService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "api-分享相关接口")
@RestController
@RequestMapping("/api/share")
public class ShareController extends BaseController {

    @Autowired
    private ICatArticletaskTableService catArticletaskTableService;

    @Autowired
    private ICatActivityTableService iCatActivityTableService;

    @Autowired
    private RichTextService richTextService;

    @Autowired
    private ICatShareuserTableService catShareuserTableService;

    @Autowired
    private ICatUserbasicTableService iCatUserbasicTableService;

    @Autowired
    private WxTokenService wxTokenService;

    private ICatChanceService catChanceService;

    @ApiOperation("文章分享列表")
    @GetMapping("getArticleList")
    public TableDataInfo getArticleList(ApiSaveShareVO apiSaveShareVO) {
        startPage();
        List<ArticletaskDTO> list = catArticletaskTableService.getActivityListApiShare();
        return getDataTable(list);
    }

    @ApiOperation("api-获取文章富文本页面")
    @GetMapping("getArticle/{id}")
    public ResponseEntity<String> generateVirtualPage(@PathVariable Long id) {
        try {
            //访问哟啊记录用户的ip等信息
            System.out.println(IpUtils.getHostIp());
            String htmlContent = richTextService.generateVirtualPage(id);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            return new ResponseEntity<>(htmlContent, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("页面生成失败");
        }
    }

    @ApiOperation("api-获取文章详情")
    @GetMapping("getArticleDetails")
    public AjaxResult getArticleDetails(Long id) {
        return success(catArticletaskTableService.selectCatArticletaskById(id));
    }

    @ApiOperation("活动推广分享列表")
    @GetMapping("getActivityList")
    public TableDataInfo getActivityList(ApiSaveShareVO apiSaveShareVO) {
        startPage();
        List<ActivityShareDTO> list = iCatActivityTableService.getActivityListApiShare();
        return getDataTable(list);
    }

    @ApiOperation("api-获取活动富文本活动介绍页面")
    @GetMapping("getActivity/{id}")
    public ResponseEntity<String> generateVirtualActivity(@PathVariable Long id) {
        try {
            //访问哟啊记录用户的ip等信息
            System.out.println(IpUtils.getHostIp());
            String htmlContent = richTextService.generateVirtualActivity(id);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            return new ResponseEntity<>(htmlContent, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("页面生成失败");
        }
    }

    @ApiOperation("api-获取活动详情")
    @GetMapping("getActivityDetails")
    public AjaxResult getActivityDetails(HttpServletRequest request,Long id) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);

  //     Long userby = catChanceService.getChanceDataByUserIdAndUserId(id,wxLoginUser.getUserId());
        ActivityTableDTO activityTableDTO = iCatActivityTableService.getActivityTableById(id,wxLoginUser.getUserId());
  /*     if (userby != null && userby > 0) {
           CatChance catChance = new CatChance();
           catChance.setUserId(wxLoginUser.getUserId());
           catChance.setArticleId(id);
           catChance.setRemainingChances(activityTableDTO.getFirstChance());
           catChanceService.insertCatChance(catChance);
       }*/
        return success(activityTableDTO);
    }


    @ApiOperation("api-文章分享记录")
    @PostMapping("saveShare")
    public AjaxResult saveShare(HttpServletRequest request, @Validated @RequestBody ApiSaveShareVO apiSaveShareVO) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        CatShareuserTable catShareuserTable = new CatShareuserTable();
        catShareuserTable.setId(wxLoginUser.getUserId());
        catShareuserTable.setArticleId(apiSaveShareVO.getId());
        return success(catShareuserTableService.insertCatShareuserTable(catShareuserTable));
    }


    @ApiOperation("api-我的团队")
    @GetMapping("getMyTeam")
    public TableDataInfo getMyTeam(HttpServletRequest request,  ApiSaveShareVO apiSaveShareVO) {
        startPage();
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        List<MyTeamDTO> list = iCatUserbasicTableService.getMyTeam(wxLoginUser.getUserId());
        return getDataTable(list);
    }


}
